#!/usr/bin/perl

# ####################EDIT BY ANDY TIAN @ 2012-02-24#########################
# ###########################################################################
#  From wrfpost modifydate.pl  wrftool.pm is needed
#  Usage1: ./ywxt.wrf.pl       
#  Usage2: ./ywxt.wrf.pl -s2008052800 -f24   
# ##########################################################################

#################### Settings ####################
#
# set env;
#
$ENV{'PATH'} = '/public/software/NCL_6.1.0/bin:/public/software/netcdf-3.6.3-pgi/bin:/public/software/openmpi-1.4.5-pgi/bin:/public/software/pgi10/linux86-64/10.0/bin:/public/software/intel/Compiler/11.1/056/bin/intel64:/public/software/intel/Compiler/11.1/056/bin/intel64:/opt/gridview/pbs//dispatcher-sched//bin:/opt/gridview/pbs//dispatcher-sched//sbin:/opt/gridview/pbs//dispatcher//bin:/opt/gridview/pbs/dispatcher/sbin:/usr/local/bin:/bin:/usr/bin:/public/users/wrf/bin:.';
#print $ENV{'PATH'};
$ENV{'TERM'} ='linux';  #Set the hash list 'ENV',which hash key 'TERM' value is equal to 'linux'
require 5;              #Set the 'perl' version that run the perl program
use strict;             #Set the compile level (strict code written way)
#umask 002;
use vars qw($opt_b $opt_e $opt_f $opt_i $opt_m $opt_o $opt_s ); #Define the variables that are be sent by outer parameters
use Getopt::Std;    #Use the module of Getopt::Std that be used to set parameters 

my ($start,$offset,$fcst,$intv,$maxsteps);
my ($h2);
# Get command line options
getopts('bef:i:m:o:s:'); # bef , i, m, o, s, all have parameter, Set the variables which defined by 'use vars wq()' parameter information
if ($opt_o){    #if the variable has non-emply value,its value be sent to $offset,or $offset to set to '0'
    $offset=$opt_o;  #the '$opt_o' value come from outer parameter
} else {
    $offset=0;
}
if ($opt_f){
    $fcst=$opt_f; # The variable 'fcst' that is forecast time 
} else {
    $fcst=72;   #Default forecast time is 72 hours 
}
if ($opt_i){
    $intv=$opt_i;   #'$intv' be used to operate the $h2(hour) value
} else {
    $intv=3;
}
if ($opt_s){ #the if struct is be used to operate '$start' format
    $start=$opt_s;
    if (! ($start =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)$/)) {   #Test the start time format (all 0~9 data)
        print "Invalid start datetime: $opt_s\n";
        exit(-1);
    }
} else {
    $offset=-12;
    $start = `date +"%Y%m%d%H"`;    #Set the system date and time display format,%Y is completely year(0000~9999),%m is month(01~12)
    chomp $start;   #Operate the format of $start variable,such as '\n'
    if ($start =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)$/) { 
        $h2=int($4/$intv)*$intv;    #'$intv' is interval value, which be used to operate the $4(hour),and the result be set to $h2
        if ($h2 > 10 )          #when '$h2',which is hour,greater than 10,'$start's value is equal to '$1$2$3$h2'
        {$start="$1$2$3$h2"}    #Now, the $h2 is the hour value,regular expressions: $1 is year(/d/d/d/d),$2 is month(/d/d),$3 is day 
        else
        {$start="$1$2$3"."0"."$h2"};    #when '$h2' less than 10,$start format will add a '0' in the time variable
    }
}

my $start_cst;
if ($start =~/^(\d\d\d\d)(\d\d)(\d\d)(\d\d)$/) { 
    my $hh_cst = $4 + 8;
    if ($hh_cst < 10)
    { $start_cst = "$1$2$3"."0"."$hh_cst" }
    else
    { $start_cst = "$1$2$3$hh_cst"; }
}

if ($opt_m){    #$maxsteps is relative to $fcst
    $maxsteps=$opt_m;
} else {
    $maxsteps=-1;
}

#################### Check if a rerun is needed ####################
my ($running, $wrfout_dir);
# if GFS data are not complete

# check running processes
$running = `ps x`;
#The names of currently running processes are stored in '$running'
#Linux system order line 'ps' for checking process state,'u' by use name and start time to show process,'x' show no control terminal process.  
if ( (( $running =~ /\/public\/users\/wrf\/bin\/wrf\.sandbox\.sh $start_cst/) or ( $running =~ /aria2c/)) and
    ($running =~ /perl\s\/public\/users\/wrf\/bin\/ywxt\.wrf\.pl -s$start/) )
{
    `date >> /tmp/wrftmp/$start.log`;
    `echo "WRF is running, exiting." >> /tmp/wrftmp/$start.log`;
    print ("WRF is running, exiting.\n");
    print ($running);
    exit 1;
}

# check wrfout files
# if (-e "/public/users/wrf/wrfout/$start/"
$wrfout_dir = "/public/users/wrf/wrfout/$start_cst";
if ($start =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)$/) {
    my ($d01_file, $d02_file, $d03_file);
    $d01_file = "$wrfout_dir/wrfout_d01_$1-$2-$3_$4:00:00";
    $d02_file = "$wrfout_dir/wrfout_d02_$1-$2-$3_$4:00:00";
    if ((-e $d01_file) and (-e $d02_file)) {
        `date >> /tmp/wrftmp/$start_cst.log`;
        `echo "wrfout files exist." >> /tmp/wrftmp/$start_cst.log`;
        print "wrfout files exist.\n";
        my $d01_size = -s $d01_file;
        if ($d01_size > 5000000000){
            print "$d01_size > 5000000000, quitting.\n";
            exit 1;
        }
    }
}
#################### Change Namelists ####################

chdir("/public/users/wrf/WRFV3/test/em_real");

my ($HOMEROOT, $NLROOT);
$HOMEROOT = `pwd`; 
chomp $HOMEROOT;
require "/public/users/wrf/bin/wrftool.pm";

$NLROOT = "$HOMEROOT";
print( $NLROOT);

my ($namelist_file, $namelist_wps);
$namelist_file = "$NLROOT/namelist.input";  #Configure file fullpath
$namelist_wps = "$NLROOT/namelist.wps";

if(not -d $NLROOT)
{
    print(" Namelist directory doesn\'t exist: $NLROOT\n");
    exit(1);
}
my ($starttime,$endtime,$rundays,$runhours);
$starttime =  &wrftool::compute_time ($start, $offset);  
#&wrftool::compute_time()
#&wrftool::compute_time is subprogram,wrftool is a module,compute_time is a function,
#& is a log to distinguish between system functions 

# if defined $opt_b, print starttime
(defined $opt_b) && printf "%10.10d\n",$starttime;
$endtime =  &wrftool::compute_time ($starttime, $fcst);
# if defined $opt_e, print endtime
(defined $opt_e) &&  printf "%10.10d\n",$endtime;
# if defined $opt_b or $opt_e, then exit
(defined $opt_e || defined $opt_b) && exit(0);

# calculate rundays and runhours , Why do calculate by the way???????
if ($maxsteps < 0) {
    $rundays = int($fcst/24);
    $runhours = $fcst - $rundays * 24;
} else { 
    $rundays = int($maxsteps/24);
    $runhours = $maxsteps - $rundays * 24;
}

my ($year_start, $month_start, $day_start, $hour_start); 
if ($starttime =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)$/) 
{
    $year_start = $1;
    $month_start = $2;
    $day_start = $3;
    $hour_start = $4;
}

my ($year_end, $month_end, $day_end, $hour_end);
if ($endtime =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)$/) 
{
    $year_end  = $1;
    $month_end = $2;
    $day_end   = $3;
    $hour_end = $4;
}

# Read in the namelist info.

open(NL,"$namelist_file") or die "Can't open $namelist_file\n";
my @lines = <NL>;
close(NL);

my %namehash = &wrftool::get_namelist_hash(@lines);

# my @namekeys = keys %namehash;
# my @namevalues = values %namehash;
# foreach (@namekeys){
#     my $key = $_;
#     my @values = @{$namehash{$key}};
#     print "\n$_ : @values";
# }

my $dom_count=${$namehash{"MAX_DOM"}}[0] if (defined @{$namehash{"MAX_DOM"}});
#eg. start_time = 2012,2012,2012 ; the hash key $namehash{"start_time"},then convert to array

print( $dom_count);
my $delta=${$namehash{"TIME_STEP"}}[0] if  (defined @{$namehash{"TIME_STEP"}});

### write namelist.wps of WPS
open(NL,"$namelist_wps") 
    or die "Can't open $namelist_wps\n";
my @lines_wps = <NL>;
close(NL);

open(NL,">$namelist_wps") or die
"Can't write new version of $namelist_wps\n";

my $wps_start_date="'"."$year_start"."-"."$month_start"."-"."$day_start"."_"."$hour_start".":00:00"."'";
my $wps_end_date="'"."$year_end"."-"."$month_end"."-"."$day_end"."_"."$hour_end".":00:00"."'";
my $line;

print($wps_start_date);
print($wps_end_date);
foreach (@lines_wps) 
{
    $line = $_;
    if (/^\s*(start_date)\s*=/i) {$line = &wrtln($1,$dom_count,$wps_start_date)} 
    #if matches "start_date", generate start_date line
    #/^\s*(start_date)\s*=/i,\s(blank),*(any numbwr),(),\s*(any number blank)
    #//i,don't care the letters are uppercase or lowercase

    if (/^\s*(end_date)\s*=/i){$line = &wrtln($1,$dom_count,$wps_end_date)}
    print NL "$line";
}
close(NL);

### write namelist.input of WRF model
open(NL,">$namelist_file") or die
"Can't write new version of $namelist_file\n";
my $line;
foreach (@lines) 
{
    $line = $_;
    if (/^\s*(time_step_max\s*)=/i) {$line = " $1= $maxsteps,\n";} # edit maxstep, not used
    if (/^\s*(run_days\s*)=/i)   {$line = " $1= $rundays,\n";}
    if (/^\s*(run_hours\s*)=/i)  {$line = " $1= $runhours,\n";}
#  if (/^\s*(start_year)\s*=/i) {$line = &wrtln($1,$count,$year_start)}
    if (/^\s*(start_year)\s*=/i) {$line = &wrtln($1,$dom_count,$year_start)}
    if (/^\s*(start_month)\s*=/i){$line = &wrtln($1,$dom_count,$month_start)}
    if (/^\s*(start_day)\s*=/i)  {$line = &wrtln($1,$dom_count,$day_start)}
    if (/^\s*(start_hour)\s*=/i) {$line = &wrtln($1,$dom_count,$hour_start)}
    if (/^\s*(end_year)\s*=/i)   {$line = &wrtln($1,$dom_count,$year_end)}
    if (/^\s*(end_month)\s*=/i)  {$line = &wrtln($1,$dom_count,$month_end)}
    if (/^\s*(end_day)\s*=/i)    {$line = &wrtln($1,$dom_count,$day_end)}
    if (/^\s*(end_hour)\s*=/i)   {$line = &wrtln($1,$dom_count,$hour_end)}
    print NL "$line";
}
close(NL);

my ($ycmd,$time0,$time1,$file_sum,$count,$gfsfile,$downpid,$downtime,$yline,$ps_axel);
my ($flen1,$flen2,$xcs);

# make log dirs
my $dirname = '/tmp/wrftmp';
unless (-d $dirname){
    system("mkdir -p /tmp/wrftmp");
}
# `date +"%F-%R" > /tmp/wrftmp/wrf.txt`; 

chdir("/public/users/wrf/ncepdata");
$time0 = `date +"%s"`;
chomp $time0;

$file_sum=0;
unless (-d $starttime){
    system("mkdir $starttime");  #Create the fold by the starttime
}
until ($file_sum == ($fcst/3+1))
{
    for($count=0;$count<=$fcst;$count=$count+3) #For three zone,so $count = $count + 3
    {
        $gfsfile="gfs.t".$hour_start."z.pgrb2f".$count; 
        #Define data file name come from meteorological data,eg.gfs.t00z.pgrb2f00
        
        if ($count < 10)
        {
            $gfsfile="gfs.t".$hour_start."z.pgrb2f0".$count;
        }
        my $gfsl="gfs.$starttime".substr($gfsfile,8);
        #from the eighth char to the end,get the substring 
        #the file name eg. gfs2012070300.pgrb2f00 ==> "gfs"."2012070300".substr(),substr() ==> pgrb2f00 
        #And $gfsl is the linker system("ln -s ./$starttime/$gfsfile ./$gfsl");

        #
        # redownload
        #
        if (-e "./$starttime/$gfsl.prozilla")
        {
            system("rm ./$starttime/$gfsl.prozilla");
        }
        if (! -e "./$starttime/$gfsl") #fold path eg:  ./2012070300/gfs.t00z.pgrb2f00
        {
#            my $downresume="";
#            my $dcmd="cd $starttime;wget -qcN $downresume ftp://ftpprd.ncep.noaa.gov/pub/data/nccf/com/gfs/prod/gfs.$starttime/$gfsfile";
            #download the original GFS model meteorological data from the necp website
            my $downresume="--quiet=true --connect-timeout=30 --max-tries=10";
            my $dcmd="aria2c $downresume -o ./$starttime/$gfsl ftp://ftpprd.ncep.noaa.gov/pub/data/nccf/com/gfs/prod/gfs.$starttime/$gfsfile ";
            
            print("Downloading GFS file: gfs.$starttime/$gfsl\n");
            system("$dcmd");  #Use the 'system' cmd to excute the linux cmd line
        }

        $file_sum++; 

    } 
    $time1 = `date +"%s"`; 
    chomp $time1; 
    exit(1) if (($time1-$time0) > 100000); #Count the time that operate all files 
} ## end until


## beginning to wrf's wps and wrf main model
chdir("/public/users/wrf/WRFV3/test/em_real");
my $gfsfile_dir = '/public/users/wrf/gfs2';
unless (-d $gfsfile_dir){
    system("mkdir -p $gfsfile_dir");
}

system("rm $gfsfile_dir/*"); 

system("ln -sf /public/users/wrf/ncepdata/$starttime/gfs* $gfsfile_dir/");
#`date +"%F %R" >>/tmp/wrftmp/wrf.txt`;
print "\n1->Run... wrf.sandbox.sh\n";
#`/public/users/wrf/bin/wrf.dryrun.sh $start_cst >& /public/users/wrf/wrflog/wrf.dryrun.$start_cst.log`;
#`/public/users/wrf/bin/wrf.sandbox.sh $start_cst "/public/users/wrf/ncepdata/$starttime" >& /public/users/wrf/wrflog/wrf.sandbox.$start_cst.log`;
`/public/users/wrf/bin/wrf.sandbox.sh $start_cst "$gfsfile_dir" "$NLROOT" >& /public/users/wrf/wrflog/wrf.sandbox.$start_cst.log`;

##delete old grib2 files
system("rm -f /public/users/wrf/ncepdata/gfs*.*b2*");


printf("----------------wrfpost----------------\n"); 
## beginning wrf post run

## execute wrfpost.sh
`/public/users/wrf/bin/run_wrfpost.sh $start_cst >& /public/users/wrf/wrflog/wrfpost.$start_cst.log`;

######################## SUBROUTINE GROUPS ##########################
# $var1 ==>> 'start_date',$count ==>> the count of ','(dom), $value ==>> 2012 
# eg. start_date = 2012,2012,2012

sub wrtln{
    my ($var1,$count,$value)=@_;
    my ($line,$i,$len);
    $len = length($var1);
    $line = " $var1 "." " x (35-$len)."= ";
    $len = length($value); 
    $len = 5 if ($len > 5);
    for ($i=1;$i<=$count;$i++) 
    {
        $line .= $value.","." " x (6-$len); 
    }
    $line .= "\n";
    return($line);
}




#my $domain_no;
#for ( $domain_no = 1; $domain_no <= 3 ; $domain_no++)
#{

#    open(NL,"/public/users/wrf/wrfpost/micaps/namelist.input") or die
#    "Can't read /public/users/wrf/wrfpost/micaps/namelist.input\n";
#    my @lines_micaps = <NL>;
#    close(NL);   
#    open(NL,">/public/users/wrf/wrfpost/micaps/namelist.input");
#    my ($line,$wrfoutd0x,$micaps_root);
#    $wrfoutd0x=$NLROOT."/wrfout_d0".$domain_no."_".$year_start."-".$month_start."-".$day_start."_".$hour_start.":00:00";
#    $micaps_root="./outdata/".$year_start.$month_start.$day_start.$hour_start."/d0".$domain_no;
#    foreach (@lines_micaps) {
#        $line = $_;
#        if (/^\s*(inputfile\s*)=/i)  {$line = "$1=\"$wrfoutd0x\"\n";}
#        if (/^\s*(DATA_ROOT\s*)=/i)  {$line = "$1=\"$micaps_root\"\n";}
#        print NL "$line";
#    }
#    close(NL);

#	if(-e $wrfoutd0x)
#	{
#		printf("----------------wrf-micaps----------------\n");
#		print "5->Run...  /public/users/wrf/wrfpost/micaps/proc.sh\n";
#		chdir("/public/users/wrf/wrfpost/micaps");  
#		system("mkdir -p $micaps_root");
#		$time0 = `date +"%s"`; chomp $time0;
#		system "./proc.sh >> /tmp/wrftmp/wrf.post.proc.micaps.txt 2>&1";     
#			$time1 = `date +"%s"`; chomp $time1; 
#		printf("time consumed this time: %d seconds\n",($time1-$time0));
    #
    #		open (NL,">>/tmp/wrftmp/wrf.post.proc.micaps.txt");
    #	printf NL "5->./proc.sh time consumed: %d seconds\n",($time1-$time0);
    #	close (NL); 
    #	my $dtime="$year_start"."$month_start"."$day_start"."$hour_start"; 
    #	printf("dtime: %s\n",$dtime);

#  system("/public/users/wrf/ftp_micaps $dtime 1>/dev/null &");
#	}
#}
#printf("----------------wrf-grads----------------\n");
#added by whwen start
#my ($domain_no);
#for ( $domain_no = 1; $domain_no <= 3 ; $domain_no++)
#{
#    open(NL,"/public/users/wrf/wrfpost/grads/namelist.input") or die
#    "Can't write /public/users/wrf/wrfpost/grads/namelist.input\n";
#    my @lines_grads = <NL>;
#    close(NL);  
#    open(NL,">/public/users/wrf/wrfpost/grads/namelist.input");
#    my ($line,$wrfoutd0x,$output_root);
#    $wrfoutd0x=$NLROOT."/wrfout_d0"."$domain_no"."_".$year_start."-".$month_start."-".$day_start."_".$hour_start.":00:00";
#    $output_root="./output/".$year_start.$month_start.$day_start.$hour_start."/d0".$domain_no;
#    foreach (@lines_grads) {
#        $line = $_;
#        if (/^\s*(inputfile\s*)=/i)  {$line = "$1=\"$wrfoutd0x\"\n";}
#        if (/^\s*(DATA_ROOT\s*)=/i)  {$line = "$1=\"$output_root\"\n";}
#        print NL "$line";
#    }
##    close(NL);
    #
#   # if(-e $wrfoutd0x)  
#   # {
#   #     my $gsout="cwrf_d03_"."$year_start"."$month_start"."$day_start"."$hour_start"."_0000";
##ge#nerate grads ctl and dat
    #    #print "6->Run...  /public/users/wrf//wrfpost/grads/proc.sh\n";
    #    #chdir("/public/users/wrf//wrfpost/grads");
    #    #$time0 = `date +"%s"`; chomp $time0;
    #    #if (! -e "$output_root/$gsout.ctl")
    #    #{
    #    #    unless (-d $output_root){
    #            #system("mkdir -p $output_root");
    #            #system "echo $output_root >/tmp/dname1";
    #            #    }
    #        #system "echo $output_root >/tmp/dname";
    #        #system "./proc.sh >> /tmp/wrftmp/wrf.post.proc.grads.txt 2>&1";
    #        #printf("----------------wrf-grads-------1---------\n");
#   #     }
    #    $time1 = `date +"%s"`; chomp $time1; 
    #    #printf("time consumed this time: %d seconds\n",($time1-$time0));
    #
##ge#nerate gif according to gs files and ctl
    #    #chdir("gs.scripts");
    #    #printf("----------------wrf-grads--------2--------\n");
    #    if(-e "../$output_root/$gsout.ctl" and -e "../$output_root/$gsout.dat")
    #    {
    #        #system "ln ../$output_root/$gsout* .";
    #        #printf("----------------wrf-grads--------3--------\n");
    #    }
    #
##ch#ange gs opening files and execute it;
    #    my $gs_;
    #    my @gsnames = ("t2.gs","rainp.gs","plevels.gs","stream.gs","rain-6.gs","rain-24.gs","raingrid.gs");
    #
    #    foreach $gs_(@gsnames)
    #    {
    #
    #        open(NL,"/public/users/wrf/wrfpost/grads/gs.scripts/$gs_") or die
    #        "Can't write /public/users/wrf/wrfpost/grads/gs.scripts/$gs_\n";
    #        my @lines_grads = <NL>;
    #        close(NL);  
    #
    #
    #        open(NL,">/public/users/wrf/wrfpost/grads/gs.scripts/$gs_"); 
    #        my $line;
    #        foreach (@lines_grads) {
    #            $line = $_;
    #            if (/^\'open /)  {$line = "\'open $gsout.ctl\'\n";}
    #            print NL "$line";
    #        }
    #        close(NL);
    #
#exe#cute gs file
    #        if (-e "$gsout.ctl")
    #        {
    #            #`rungs.sh $gs_`;
    #            #printf("----------------wrf-grads-----4-----------\n");
    #        }
    #    }
# 
# move all gif to a directory
        #
#        my $dtime="$year_start"."$month_start"."$day_start"."$hour_start"; 
#  system("mkdir -p ../output/$dtime");
#  system("mv *.gif ../output/$dtime"); 
#        my $gif_root = "../gifout/".$year_start.$month_start.$day_start.$hour_start."/d0".$domain_no;
#        unless (-d $gif_root){
#            system("mkdir -p $gif_root");
#        }
#        system("mv *.gif $gif_root/"); 
#        unlink("$gsout.ctl");
#        unlink("$gsout.dat");
        #
        #open (NL,">>/tmp/wrftmp/wrf.post.proc.grads.txt");
        #printf NL "6->./proc.sh time consumed this time: %d seconds\n",($time1-$time0);
        #close (NL); 
#		#system("/public/home/wrf/ftp_gif $dtime");
        #}
#}

#my ($wrfoutput,$gradout,$micapsout);
#$wrfoutput = "wrfout_".$starttime."."."tgz";
#$gradout = "/public/home/wrf/wrfv3.1/wrfpost/micaps/outdata/".$starttime
#$micapsout = "/public/home/wrf/wrfv3.1/wrfpost/grads/output".$starttime
#system("tar zcf $wrfoutput ");
